package web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import entity.Admin;
import entity.Product;
import entity.User;
import enums.ProductEnum;
import enums.travelenums;
import service.File_service;
import service.Product_service;
import util.PropertiesUtil;

@Controller

public class Product_controller {
	
	@Autowired
	private Product_service product_service;
	@Autowired
	private File_service file_service;
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	public String index(@RequestParam(value="page",defaultValue="1",required=true)Integer page,Model model){
		Page<Product> pa=PageHelper.startPage(page, 9);
		List<Product> list=product_service.getlistByStatus(ProductEnum.ONSALE.getState());//在售商品列表
		PageInfo<Product> pageinfor=new PageInfo<Product>(pa);
		model.addAttribute("page", pageinfor);
		model.addAttribute("list",list);
		return "index";
	}
	
	@RequestMapping(value="detail")
	public String detail(@RequestParam("id") int id,Model model){
		Product product= product_service.getproByid(id);
		model.addAttribute("product_detail",product);
		return "product";
	}
	
	@RequestMapping(value="searchproduct",method=RequestMethod.POST)
	public String SearchProduct(@RequestParam(value="proName",required=false)String proName,
					@RequestParam(value="page",defaultValue="1",required=true)Integer page,Model model){		
		
		proName=new StringBuffer().append("%").append(proName).append("%").toString();
		Page<Product> pa=PageHelper.startPage(page, 9);
		List<Product> list=product_service.getproByName(proName);
		PageInfo<Product> pageinfor=new PageInfo<Product>(pa);
		model.addAttribute("page", pageinfor);
		List<Product> onsalelist=new ArrayList<Product>();
		for (Product product : list) {
			if(product.getStatus()==ProductEnum.ONSALE.getState()){
				onsalelist.add(product);
			}
		}
		model.addAttribute("list", onsalelist);
		return "index";
	}
	
	@RequestMapping(value="showfood",method=RequestMethod.POST)
	public String showFood(@RequestParam("productId")Integer productId,Model model){
		Product product= product_service.getproByid(productId);
		model.addAttribute("food", product.getFood());
		return "food";
	}
	
	@RequestMapping(value="showroute",method=RequestMethod.POST)
	public String showRoute(@RequestParam("productId")Integer productId,Model model){
		Product product= product_service.getproByid(productId);
		model.addAttribute("route", product.getRoute());
		return "route";
	}
	
	/*后台*/
	
	@RequestMapping(value="productlist",method=RequestMethod.POST)
	public String productlist(@RequestParam(value="page",defaultValue="1",required=true)Integer page,
							Model model){
		Page<Product> pa=PageHelper.startPage(page, 8);
		List<Product> list=product_service.getallproduct();
		PageInfo<Product> pageinfor=new PageInfo<Product>(pa);
		model.addAttribute("page", pageinfor);
		model.addAttribute("pro_list", list);
		return "back/backproduct";
	}
	
	@RequestMapping(value="editproduct",method=RequestMethod.POST)
	public String editproduct(@RequestParam(value="id",required=false)Integer proid,Model model){		
		if(proid!=null){
			Product product=product_service.getproByid(proid);
			model.addAttribute("pro", product);
		}	
		return "back/editproduct";
	}
		/*这里的id指（真正的id（porid，oderid），可以是别的obj，string）前段函数复用的结果*/
	@RequestMapping(value="backsearchproduct",method=RequestMethod.POST)
	public String BackSearchProduct(@RequestParam(value="id",required=false)String proName,
					@RequestParam(value="page",defaultValue="1",required=true)Integer page,Model model){		
		proName=new StringBuffer().append("%").append(proName).append("%").toString();
		Page<Product> pa=PageHelper.startPage(page, 8);
		List<Product> list=product_service.getproByName(proName);
		PageInfo<Product> pageinfor=new PageInfo<Product>(pa);
		model.addAttribute("page", pageinfor);
		model.addAttribute("pro_list", list);
		return "back/backproduct";
	}
	
	@RequestMapping(value="upload",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
	@ResponseBody
	public String upload(MultipartFile file,HttpServletRequest request,HttpSession session){
		Admin admin=(Admin)session.getAttribute("admin");
		if(admin!=null){
			String path=request.getSession().getServletContext().getRealPath("upload");
			String targetFileName=file_service.upload(file, path);
			String url=PropertiesUtil.getProperty("ftp.http.prefix")+targetFileName;
			return url;		
		}
		return travelenums.NOLOGIN.getInfo();
	}
	@RequestMapping(value="deletefile",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
	@ResponseBody
	public String deleteFile(String file,HttpServletRequest request,HttpSession session){
		
		Admin admin=(Admin)session.getAttribute("admin");
		if(admin!=null){			
			file_service.delete(file);
			return travelenums.SUCCESS.getInfo();
		}
		return travelenums.NOLOGIN.getInfo();
	}
	
	@RequestMapping(value="richtext/upload",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
	@ResponseBody
	public String richTextUpload(@RequestParam("file")MultipartFile file,HttpServletRequest request,HttpSession session){
		Admin admin=(Admin)session.getAttribute("admin");
		if(admin!=null){
			String path=request.getSession().getServletContext().getRealPath("upload");
			String targetFileName=file_service.upload(file, path);
			String url=PropertiesUtil.getProperty("ftp.http.prefix")+targetFileName;
			return url;		
		}
		return travelenums.NOLOGIN.getInfo();
	}
	
	@RequestMapping(value="update/product",method=RequestMethod.POST)
	@ResponseBody
	public String updateOrInsertProduct(HttpSession session,Product product){
		Admin admin=(Admin)session.getAttribute("admin");
		if(admin!=null){
			boolean isSuccess=product_service.updateOrInsertProduct(product);
			if(isSuccess){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.NOLOGIN.getInfo();
	}
	
	@RequestMapping(value="product/status")
	@ResponseBody
	public String setProductStatus(Integer productId,Integer status,HttpSession session){
		Admin admin=(Admin)session.getAttribute("admin");
		if(admin!=null){
			boolean boo=product_service.setProductStatus(productId, status);
			if (boo) {
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.NOLOGIN.getInfo();
	}
	
	@RequestMapping(value="delete/product",method=RequestMethod.POST)
	@ResponseBody
	public String deleteProduct(Integer productId,HttpSession session){
		Admin admin=(Admin)session.getAttribute("admin");
		if(admin!=null){
			boolean boo=product_service.deleteProduct(productId);
			if(boo){
				return travelenums.SUCCESS.getInfo();
			}
			return travelenums.ERRO.getInfo();
		}
		return travelenums.NOLOGIN.getInfo();
	}
	
}
